package com.example.controller;


import com.example.common.Result;
import com.example.dao.*;
import com.example.entity.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin//解决跨域问题注解
@RestController
public class StatisticsController {
    @Resource
    NewsDao newsDao;
    @Resource
    CategoryDao categoryDao;
    @Resource
    GoodsDao goodsDao;
    @Resource
    OrdermsgDao ordermsgDao;
    @Resource
    OrderdateilDao orderdateilDao;
    @Resource
    MemberDao memberDao;

    //各菜品销量统计{柱状图}
    @ResponseBody
    @RequestMapping("bar")
    public Result bar(HttpServletRequest request) {
        HashMap<String,Object> res = new HashMap<String,Object>();
        HashMap map = new HashMap();
        List<Goods> olist = goodsDao.selectAll(map);
        ArrayList xdata = new ArrayList();
        ArrayList ydata = new ArrayList();
        for(Goods goods:olist){
            map.put("goodsid",goods.getId());
            int slnum = 0;
            List<Orderdateil> commlist = orderdateilDao.selectAll(map);
            for(Orderdateil orderdateil:commlist){
                slnum+=orderdateil.getSlnum();
            }
            xdata.add(goods.getName());
            ydata.add(slnum);
        }
        res.put("xdata", xdata);
        res.put("ydata", ydata);
        return Result.success(res);
    }

    //菜品分类统计
    @ResponseBody
    @RequestMapping("bie")
    public Result tjorder(HttpServletRequest request) {
        List dataval = new ArrayList();
        List<Category> list = categoryDao.selectAll(null);
        for(Category category:list){
            HashMap map1 = new HashMap();
            map1.put("key1",category.getId());
            List<Goods> glist = goodsDao.selectAll(map1);
            map1.put("value", glist.size());
            map1.put("name", category.getName());
            dataval.add(map1);
        }
        return Result.success(dataval);
    }

    //每日销售额统计
    @ResponseBody
    @RequestMapping("tjMoney")
    public Result tjMoney(HttpServletRequest request) {
        HashMap<String,Object> res = new HashMap<String,Object>();
        String key = request.getParameter("key");
        HashMap map = new HashMap();
        map.put("status", "待付款");
        List<Ordermsg> olist = ordermsgDao.selectTj(map);
        //ArrayList nslist = new ArrayList();
        ArrayList nlist = new ArrayList();
        ArrayList slist = new ArrayList();
        for(Ordermsg ordermsg:olist){
            nlist.add(ordermsg.getSavetime());
            slist.add(ordermsg.getTotal());
        }
        res.put("xmaindata", nlist);
        res.put("ymaindata", slist);
        return Result.success(res);
    }

    //后台首页数量统计
    @ResponseBody
    @RequestMapping("home")
    public Result home(HttpServletRequest request) {
        HashMap<String,Object> res = new HashMap<String,Object>();
        //查询用户数量
        List<Member> mlist = memberDao.selectAll(null);
        //查询菜品数量
        List<Goods> glist = goodsDao.selectAll(null);
        //查询新闻公告数量
        List<News> xlist = newsDao.selectAll(null);
        //查询订单数量
        List<Ordermsg> olist = ordermsgDao.selectAll(null);
        res.put("mlist", mlist);
        res.put("glist", glist);
        res.put("xlist", xlist);
        res.put("olist", olist);
        return Result.success(res);
    }


}
